package com.shangcheng.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shangcheng.dubbo.mappers.AdminRoleRelationMapper;
import com.shangcheng.model.domain.AdminRoleRelation;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;

@DubboService
public class AdminRoleRelationApiImpl implements AdminRoleRelationApi{

    @Autowired
    private AdminRoleRelationMapper adminRoleRelationMapper;

    //获取指定用户的角色,查询中间表中所有关于该用户的角色
    @Override
    public List<AdminRoleRelation> getRole(Integer adminId) {
        LambdaQueryWrapper<AdminRoleRelation> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdminRoleRelation::getAdminId,adminId);
        List<AdminRoleRelation> list = adminRoleRelationMapper.selectList(lambdaQueryWrapper);
        return list;
    }

    //删除当前用的角色
    @Override
    public void removeById(Integer adminId) {
        LambdaQueryWrapper<AdminRoleRelation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AdminRoleRelation::getAdminId, adminId);
        adminRoleRelationMapper.delete(wrapper);
    }

    //给用户添加角色
    @Override
    public void insert(Integer adminId, List<Long> longList) {
        for (Long roleId : longList) {
            AdminRoleRelation adminRoleRelation = new AdminRoleRelation();
            adminRoleRelation.setAdminId(Long.valueOf(adminId));
            adminRoleRelation.setRoleId(roleId);
            adminRoleRelationMapper.insert(adminRoleRelation);
        }
    }
}
